home *** CD-ROM | disk | FTP | other *** search
/ Delphi Magazine Collection 2001 / Delphi Magazine Collection 20001 (2001).iso / DISKS / Issue40 / Threads / ThreadsDemo.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1998-10-28  |  2.6 KB  |  118 lines

  1. unit ThreadsDemo;
  2.  
  3. interface
  4.  
  5. uses
  6.   Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  7.   StdCtrls,
  8.   HVSyncObjs,
  9.   HVBackgroudThread
  10.   ;
  11.  
  12. type
  13.   TSoundDemoForm = class(TForm)
  14.     AddedTasksList: TListBox;
  15.     DoneTasksList: TListBox;
  16.     InBoxList: TListBox;
  17.     OutBoxList: TListBox;
  18.     AddButton: TButton;
  19.     Label1: TLabel;
  20.     Label2: TLabel;
  21.     Label3: TLabel;
  22.     Label4: TLabel;
  23.     ClearButton: TButton;
  24.     MultithreadedCB: TCheckBox;
  25.     procedure AddButtonClick(Sender: TObject);
  26.     procedure ClearButtonClick(Sender: TObject);
  27.     procedure FormCreate(Sender: TObject);
  28.   private
  29.     { Private declarations }
  30.     procedure UpdateTStringsFromThreadList(Strings: TStrings; ThreadList: TWaitableThreadList);
  31.     procedure UpdateAllLists;
  32.     procedure SoundTaskDone(Task: TBackgroundTask);
  33.   public
  34.     { Public declarations }
  35.   end;
  36.  
  37. var
  38.   SoundDemoForm: TSoundDemoForm;
  39.  
  40. implementation
  41.  
  42. {$R *.DFM}
  43.  
  44. uses
  45.   HVSounds
  46.   ;
  47.  
  48. function IntToSound(SoundIndex: integer): string;
  49. begin
  50.   Result := 'Sound '+IntToStr(SoundIndex);
  51. end;
  52.  
  53. procedure TSoundDemoForm.UpdateTStringsFromThreadList(Strings: TStrings; ThreadList: TWaitableThreadList);
  54. var
  55.   i : integer;
  56.   SoundTask: TPlaySoundTask;
  57. begin
  58.   Strings.BeginUpdate;
  59.   try
  60.     Strings.Clear;
  61.     with ThreadList.List.LockList do
  62.       try
  63.         for i := 0 to Count-1 do
  64.         begin
  65.           SoundTask := TPlaySoundTask(List^[i]);
  66.           Strings.Add(IntToSound(SoundTask.SoundIndex));
  67.         end;
  68.       finally
  69.         ThreadList.List.UnlockList;
  70.       end;
  71.   finally
  72.     Strings.EndUpdate;
  73.   end;
  74. end;
  75.  
  76. procedure TSoundDemoForm.UpdateAllLists;
  77. begin
  78.   with PlaySoundThread do
  79.   begin
  80.     UpdateTStringsFromThreadList(InBoxList.Items, InBox);
  81.     UpdateTStringsFromThreadList(OutBoxList.Items, OutBox);
  82.   end;
  83. end;
  84.  
  85. procedure TSoundDemoForm.SoundTaskDone(Task: TBackgroundTask);
  86. begin
  87.   with Task as TPlaySoundTask do
  88.   begin
  89.     DoneTasksList.Items.Add(IntToSound(SoundIndex));
  90.     UpdateAllLists;
  91.   end;
  92. end;
  93.  
  94. procedure TSoundDemoForm.FormCreate(Sender: TObject);
  95. begin
  96.   Randomize;
  97. end;
  98.  
  99. procedure TSoundDemoForm.AddButtonClick(Sender: TObject);
  100. var
  101.   SoundIndex : integer;
  102. begin
  103.   SoundIndex := Random(MaxSounds)+1;
  104.   AddedTasksList.Items.Add(IntToSound(SoundIndex));
  105.   UpdateAllLists;
  106.   PlaySoundIndex(SoundIndex, Self.SoundTaskDone, MultithreadedCB.Checked);
  107. end;
  108.  
  109. procedure TSoundDemoForm.ClearButtonClick(Sender: TObject);
  110. begin
  111.   AddedTasksList.Items.Clear;
  112.   InBoxList.Items.Clear;
  113.   OutBoxList.Items.Clear;
  114.   DoneTasksList.Items.Clear;
  115. end;
  116.  
  117. end.
  118.